// stores/system.js - 增强的Pinia系统配置状态管理
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import systemApi from '@/api/system'
import { ElMessage } from 'element-plus'

export const useSystemStore = defineStore('system', () => {
    // 系统状态
    const systemStatus = ref({
        version: '',
        status: 'unknown',
        apiStatus: 'unknown',
        emailStatus: 'unknown'
    })

    // 加载状态
    const loading = ref(false)

    // 获取系统状态
    const getSystemStatus = async () => {
        try {
            const response = await systemApi.getSystemStatus()
            systemStatus.value = response.data || response
            return systemStatus.value
        } catch (error) {
            console.error('获取系统状态失败:', error)
            ElMessage.error('获取系统状态失败')
            throw error
        }
    }

    // 检查系统连接状态（兼容旧版本）
    const checkSystemStatus = async () => {
        try {
            await getSystemStatus()
            return systemStatus.value.status === 'running' ? 'online' : 'offline'
        } catch (error) {
            return 'offline'
        }
    }

    return {
        // 状态
        systemStatus,
        loading,

        // 方法
        getSystemStatus,
        checkSystemStatus
    }
})